package akayblog;

import java.util.List;

import akayblog.EntryComponent.MODE;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.BaseTheme;

public class MainWindow extends Window {

	public static List<Entry> entryList;

	private Label label;

	private Button button;

	private HorizontalLayout horizontalLayout;

	private HorizontalLayout horizontalLayout2;

	private VerticalLayout verticalLayout;

	private VerticalLayout verticalLayout2;

	private SubWindow subWindow;

	private BlogService service;

	private MyTree tree;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public MainWindow() {
		buildMainWindow();
	}

	public void buildMainWindow() {
		setWidth("50%");
		setHeight("100%");
		service = new BlogService();

		label = new Label("<h1>Akayblog.com'a Hoşgeldiniz</h1>");
		label.setContentMode(Label.CONTENT_XHTML);

		button = new Button("Yeni kayıt");
		button.setStyleName(BaseTheme.BUTTON_LINK);

		addButtonListener();

		horizontalLayout = new HorizontalLayout();
		horizontalLayout.setWidth("100%");
		horizontalLayout.setHeight("10%");
		horizontalLayout.addComponent(label);
		horizontalLayout.setComponentAlignment(label, Alignment.TOP_CENTER);
		horizontalLayout.addComponent(button);
		horizontalLayout.setComponentAlignment(button, Alignment.TOP_RIGHT);

		verticalLayout = new VerticalLayout();
		verticalLayout.setMargin(true);
		verticalLayout.setSpacing(true);
		verticalLayout.setWidth("100%");
		verticalLayout.setWidth("100%");

		listEntry();
		addComponent(horizontalLayout);

		horizontalLayout2 = new HorizontalLayout();
		horizontalLayout2.setSpacing(true);
		horizontalLayout2.setSpacing(true);

		horizontalLayout2.addComponent(verticalLayout);
		tree = new MyTree(horizontalLayout2, this.getWindow());

		addComponent(horizontalLayout2);

		verticalLayout2 = new VerticalLayout();
		verticalLayout2.addComponent(horizontalLayout);
		verticalLayout2.addComponent(horizontalLayout2);

		setContent(verticalLayout2);

	}

	public void refresh() {
		entryList = service.getEntryList();
		tree.buildMyTree();
		verticalLayout2.requestRepaintAll();
	}

	public void addButtonListener() {

		button.addListener(new ClickListener() {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				subWindow = new SubWindow(verticalLayout, MainWindow.this);
				subWindow.setWidth("40%");
				subWindow.setHeight("58%");

				addWindow(subWindow);

			}
		});

	}

	public void listEntry() {
		entryList = service.getEntryList();
		for (Entry entry : entryList) {
			EntryComponent ec = new EntryComponent(entry);
			addEntryComponentListeners(ec);
			verticalLayout.addComponent(ec, 0);
		}
	}

	private void addEntryComponentListeners(final EntryComponent ec) {

		ec.getSave().addListener(new ClickListener() {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				service.updateEntry(ec.getEntry());
				ec.setCurrentMode(MODE.READ);

			}
		});
		ec.getRemove().addListener(new ClickListener() {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				service.deleteEntry(ec.getEntry());
				verticalLayout.removeComponent(ec);
				refresh();

			}
		});

	}

	// public void treeAddListener() {
	// final List<Entry> entries = service.getEntryList();
	// tree.addListener(new ItemClickEvent.ItemClickListener() {
	// public void itemClick(ItemClickEvent event) {
	// // Pick only left mouse clicks
	// if (event.getButton() == ItemClickEvent.BUTTON_LEFT)
	// for (Entry entry : entries) {
	// if (entry.getTitle() == event.getItemId()) {
	//
	// final Window window = new Window();
	//
	// final RichTextArea area = new RichTextArea();
	// final Button close = new Button("Kapat",
	// new Button.ClickListener() {
	//
	// @Override
	// public void buttonClick(ClickEvent event) {
	// removeWindow(window);
	// }
	// });
	//
	// window.setWidth("50%");
	// window.setHeight("100%");
	// window.setModal(true);
	// window.addComponent(area);
	// area.setWidth("100%");
	// window.addComponent(close);
	// addWindow(window);
	// window.setCaption(entry.getTitle());
	// area.setValue(entry.getText());
	// area.setReadOnly(true);
	//
	// }
	// }
	// }
	// });
	//
	// }

}
